#! python
#! coding: utf-8

__author__ = 'Anton Vakhrushev'

import wx
from lib.extra import utils

class SingleviewForm(wx.Dialog):
    def __init__(self, parent, photos, index):
        wx.Dialog.__init__(self, parent, title=u'Просмотр фотографии')

        self.photos = photos
        self.index = index

        self.image = wx.StaticBitmap(self)

        self.box = wx.BoxSizer(wx.VERTICAL)
        # adding stretchable space before and after centers the image.
        self.box.AddStretchSpacer(1)
        self.box.Add(self.image, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 0)
        self.box.AddStretchSpacer(1)

        self.image.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        self.SetWindowStyle(0)
        self.SetBackgroundColour(wx.Colour(25, 25, 25))
        self.Maximize()
        self.SetSizerAndFit(self.box)
        self.Center(wx.CENTER_ON_SCREEN)
        self.ShowPhoto()

    def OnKeyDown(self, event):
        if event.GetKeyCode() == wx.WXK_ESCAPE:
            self.EndModal(wx.ID_OK)
        if event.GetKeyCode() == wx.WXK_RIGHT:
            self.index = (self.index + 1) % len(self.photos)
            self.ShowPhoto()
        if event.GetKeyCode() == wx.WXK_LEFT:
            self.index = (self.index - 1) % len(self.photos)
            self.ShowPhoto()
        if event.GetKeyCode() == wx.WXK_HOME:
            self.index = 0
            self.ShowPhoto()
        if event.GetKeyCode() == wx.WXK_END:
            self.index = len(self.photos) - 1
            self.ShowPhoto()

    @utils.long_time
    def ShowPhoto(self):
        photo = self.photos[self.index]
        w, h = wx.GetDisplaySize()
        img = photo.wximage(max(w, 1200), max(h, 1200))
        img = utils.resize(img, w, h, wx.IMAGE_QUALITY_BICUBIC)
        self.image.SetBitmap(wx.BitmapFromImage(img))
        self.SetTitle(u'{} {}x{}'.format(photo.title, img.GetWidth(), img.GetHeight()))
        self.image.Center()
        self.Refresh()
